{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-10-24T08:37:33.795243Z",
     "start_time": "2025-10-24T08:37:32.743082Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "df1 = pd.DataFrame([['李明','M',18,'北京'],\n",
    "                    ['张华','M',19,'天津'],\n",
    "                    ['刘涛','F',20,'上海'],\n",
    "                    ['王阳','F',14,'广州'],\n",
    "                    ['李春','F',16,'深圳']],\n",
    "                    index=['one', 'two', 'three', 'four','five'],\n",
    "                    columns=['name','gender','age','city'])\n",
    "df1"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "      name gender  age city\n",
       "one     李明      M   18   北京\n",
       "two     张华      M   19   天津\n",
       "three   刘涛      F   20   上海\n",
       "four    王阳      F   14   广州\n",
       "five    李春      F   16   深圳"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>gender</th>\n",
       "      <th>age</th>\n",
       "      <th>city</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>李明</td>\n",
       "      <td>M</td>\n",
       "      <td>18</td>\n",
       "      <td>北京</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>张华</td>\n",
       "      <td>M</td>\n",
       "      <td>19</td>\n",
       "      <td>天津</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>刘涛</td>\n",
       "      <td>F</td>\n",
       "      <td>20</td>\n",
       "      <td>上海</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>four</th>\n",
       "      <td>王阳</td>\n",
       "      <td>F</td>\n",
       "      <td>14</td>\n",
       "      <td>广州</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>five</th>\n",
       "      <td>李春</td>\n",
       "      <td>F</td>\n",
       "      <td>16</td>\n",
       "      <td>深圳</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-24T08:59:01.910616Z",
     "start_time": "2025-10-24T08:59:01.871849Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = df1['name'][:3]\n",
    "print(\"name前三行数据\")\n",
    "print(data)\n",
    "data1 = df1[['name','age']][:3]\n",
    "print(\"name,age前三行数据\")\n",
    "print(data1)\n",
    "data2=df1[1:3]\n",
    "print('访问第1-2行的数据：')\n",
    "display(data2)"
   ],
   "id": "a49efb9c4726b308",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name前三行数据\n",
      "one      李明\n",
      "two      张华\n",
      "three    刘涛\n",
      "Name: name, dtype: object\n",
      "name,age前三行数据\n",
      "      name  age\n",
      "one     李明   18\n",
      "two     张华   19\n",
      "three   刘涛   20\n",
      "访问第1-2行的数据：\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "      name gender  age city\n",
       "two     张华      M   19   天津\n",
       "three   刘涛      F   20   上海"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>gender</th>\n",
       "      <th>age</th>\n",
       "      <th>city</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>张华</td>\n",
       "      <td>M</td>\n",
       "      <td>19</td>\n",
       "      <td>天津</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>刘涛</td>\n",
       "      <td>F</td>\n",
       "      <td>20</td>\n",
       "      <td>上海</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data",
     "jetTransient": {
      "display_id": null
     }
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-24T09:01:49.474137Z",
     "start_time": "2025-10-24T09:01:49.462556Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data3 = df1.loc['three',['name','age']]\n",
    "data3"
   ],
   "id": "bd3475ca22e8d37d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name    刘涛\n",
       "age     20\n",
       "Name: three, dtype: object"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-24T09:02:54.079339Z",
     "start_time": "2025-10-24T09:02:54.055305Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data4 = df1.loc['two':'three',['name','age']]\n",
    "data4"
   ],
   "id": "8185cf7b56ba4d7e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "      name  age\n",
       "two     张华   19\n",
       "three   刘涛   20"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>张华</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>刘涛</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-24T09:20:29.924704Z",
     "start_time": "2025-10-24T09:20:29.915194Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data5 = df1.iloc[2,[3,0,1]]\n",
    "data5"
   ],
   "id": "67174bc611616a04",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "city      上海\n",
       "name      刘涛\n",
       "gender     F\n",
       "Name: three, dtype: object"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "8f8aa65c8fcf628d"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
